package com.sxintelbox;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.kongqw.serialportlibrary.Device;
import com.sxintelbox.serialPort.UGComBean;
import com.sxintelbox.serialPort.UGSerailManager;


import java.util.ArrayList;
import java.util.List;

public  class SerialPortModule  extends ReactContextBaseJavaModule  {

    private UGSerailManager serialJob;// = new UGSerailManager();;

    public SerialPortModule(@Nullable ReactApplicationContext reactContext) {

        super(reactContext);
        serialJob = new UGSerailManager(reactContext);
    }

    @ReactMethod
    public void scanDevices(Callback callback){
        serialJob.scanDevices(devices -> {
            List<String> list = new ArrayList<>();
            for (Device device : devices) {
                list.add(device.getFile().getPath());

            }
            callback.invoke(list);
        });
    }
    @ReactMethod
    public void openAll(String address){
        UGSerailManager.UGComTask task = serialJob.createTask();
        byte d = Byte.parseByte(address, 16);
        UGComBean bean = task.openAll(d); //1号从机  第12路
        task.setComBean(bean);

        serialJob.excuteTask(task);
    }

    @ReactMethod
    public void openLock(String address, String lock){
        UGSerailManager.UGComTask task = serialJob.createTask();
        byte d = Byte.parseByte(address, 16);
        byte l = Byte.parseByte(lock, 16);
        UGComBean bean = task.open(d, l); //1号从机  第12路
        task.setComBean(bean);

        serialJob.excuteTask(task);
    }

    @ReactMethod
    public void checkAll(String address) {
        UGSerailManager.UGComTask task = serialJob.createTask();
        byte d = Byte.parseByte(address, 16);
        UGComBean bean = task.status(d); //1号从机  第12路
        task.setComBean(bean);
        serialJob.excuteTask(task);
    }

    @ReactMethod
    public void checkLock(String address, String lock) {
        UGSerailManager.UGComTask task = serialJob.createTask();
        byte d = Byte.parseByte(address, 16);
        byte l = Byte.parseByte(lock, 16);
        UGComBean bean = task.status(d, l); //1号从机  第12路
        task.setComBean(bean);
        serialJob.excuteTask(task);
    }

    private void sendEvent(ReactContext reactContext,
                           String eventName,
                           @Nullable Object data) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, data);
    }
    @ReactMethod
    public void connectDevice(String deviceName, Promise promise){
        serialJob.scanDevices(devices -> {
            if (devices.isEmpty()) {
                promise.reject("No device found");
                return;
            }
            List<String> list = new ArrayList<>();
            for (Device device : devices) {
                list.add(device.getFile().getPath());
                if (device. getFile(). getPath().equals(deviceName)) {
                    serialJob.connect(device.getFile(), 9600, new UGSerailManager.DeviceConnectListener() {
                        @Override
                        public void onConnect(boolean success) {
                            UiThreadUtil.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    promise.resolve(success);
                                }
                            });
                        }

                        @Override
                        public void onMessageReceived(String data) {
                            UiThreadUtil.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    WritableMap params = Arguments.createMap();
                                    params.putString("data", data);
                                    sendEvent(getReactApplicationContext(), "onMessageReceived", params);
                                }
                            });
                        }

                        @Override
                        public void onMessageSend(String data) {
                            UiThreadUtil.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    WritableMap params = Arguments.createMap();
                                    params.putString("data", data);
                                    sendEvent(getReactApplicationContext(), "onMessageSend", params);
                                }
                            });
                        }
                    });
                }
            }
        });
    }

    @NonNull
    @Override
    public String getName() {
        return "SerialPortModule";
    }
}

